<!-- 对元素进行居中 -->
<!-- 1.使用绝对定位 (这种方法 只适合已知子元素长宽得情况)
     2. 使用浮动配合相对定位进行水平居中（这种就可以不知道子元素得长宽，但需要使用一层包裹层进行相对定位）
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .parent {
            width: 400px;
            height: 400px;
            position: relative;
            border: 1px solid red;
        }

        .child {
            position: absolute;
            width: 20px;
            height: 20px;
            left: 50%;
            top: 50%;
            margin-left: -10px;
            margin-top: -10px;
            border: 1px solid green;
        }
        .wrapper{
            position: relative;
            float: left;
            left: 50%;
            top: 50%;
            /* right: 50%; */
        }
        .child1{
            position: relative;
            left:-50%;
            top:-50%;
            border: 1px solid green;
        }
        .child2{ 
            /* 水平居中 */
            width: 40px;
            height: 40px;
            margin: 0 auto;
            border: 1px solid green;
        }
        .parent1{
            width: 400px;
            height: 400px;
            border: 1px solid red;
        }
        .child3{
            width: 200px;
            height: 200px;
            position: relative;
            left:50%;
            top:50%;
            /* transform: translate(-50%,-50%,0);
             */
             /* 进行transform转换（此时可以不知道元素的长宽，待到要解析时，动态获取进行上移和左移一般的长度） */
             transform: translate3d(-50%,-50%,0);
            /* transform: translateX(-50%); */
            background-color: red;
        }
        .parent2{
            width: 400px;
            height:400px;
            display:flex;
            justify-content: center;
            align-items: center;
            border: 1px solid red;
        }
        .child4{
            width: 200px;
            height: 200px;
            background-color: green;
        }
    </style>
</head>

<body>
    <div class="parent">
        <div class="child">

        </div>
    </div>

    <div class="parent">
        <div class="child2">

        </div>
    </div>

    <div class="parent1">
        <div class="child3">

        </div>
    </div>

    <div class="parent2">
        <div class="child4">

        </div>
    </div>

    <div class="parent">
        <div class="wrapper">
            <div class="child1" style="width: 50px;height: 50px;">

            </div>
        </div>

    </div>
    <div class="parent">
        <div class="wrapper">
            <div class="child1" style="width: 50px;height: 50px;">

            </div>
        </div>

    </div>
    <div class="parent">
        <div class="wrapper">
            <div class="child1" style="width: 100px;height: 100px;">

            </div>
        </div>

    </div>
</body>

</html>